<template>
  <div class="container">
    <div class="main">
      <!-- 整个注册盒子 -->
      <div class="loginbox">
        <!-- 左侧的注册盒子 -->
        <div class="loginbox-in">
          <div class="userbox">
            <span class="iconfont">&#xe817;</span>
            <input  class="username" id="username"   v-model="username" placeholder="用户名">
          </div>
          <br>
          <div class="idbox">
            <span class="iconfont">&#xe775;</span>
            <input  class="account" id ="account" type="account"  v-model="account" placeholder="账号">
          </div>
          <br>
          <div class="pwdbox">
            <span class="iconfont">&#xe775;</span>
            <input  class="password" id ="password" type="password"  v-model="password" placeholder="密码">
          </div>
          <br>

          <div class="log-box">
            <button type="primary"  class="login_btn" @click="login">Login</button>
          </div>
          <br>
          <button type="primary"  class="register_btn" @click="register">若无账号请点击注册</button>
        </div>
        <!-- 右侧的注册盒子 -->
        <div class="background">
          <div class="title">Welcome to Todo List</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Login',
  data () {
    return {
      username: '',
      password: '',
      account: '',
      avatar: 1
    }
  },
  created: {

  },
  methods: {
    register () { this.$router.push('Register') },
    login (event) {
      event.preventDefault()

      if (this.username.length === 0) {
        this.$alert('用户名不能为空！', '注意', {
          confirmButtonText: 'OK'
        })
        console.error('用户名为空!')
        return
      }
      // 检查账号
      if (this.account.length === 0) {
        this.$alert('账号不能为空！', '注意', {
          confirmButtonText: 'OK'
        })
        console.error('账号为空!')
        return
      }
      // 检查账号长度
      if (this.account.length !=7 ) {
        this.$alert('输入的账号需为7位!', '注意', {
          confirmButtonText: 'OK'
        })
        console.error('账号长度不正确!')
        return
      }
      // 检查密码
      if (this.password.length === 0) {
        this.$alert('密码不能为空！', '注意', {
          confirmButtonText: 'OK'
        })
        console.error('密码为空!')
        return
      }
      // 检查密码长度
      if (this.password.length < 6 || this.password.length > 12) {
        this.$alert('输入的密码需为6-12位!', '注意', {
          confirmButtonText: 'OK'
        })
        console.error('密码长度不正确!')
        return
      }

      const formData = new FormData()
      formData.append('username', this.username)
      formData.append('password', this.password)
      formData.append('account', this.account)
      // 发送POST请求到后端
      axios.post('/api/login', formData)
        .then(response => {
          // 登录成功，处理响应
          console.log(response.data)
          // 可以在这里进行重定向或其他操作
          this.$store.dispatch('saveUserInfo', response.data.data.user)
          this.$router.push('mainpage')
        })
        .catch(() => {
          // 登录失败，处理错误
          console.error('登录失败')
          this.$alert('账号或密码错误！', '注意', {
          confirmButtonText: 'OK'
        })
        })
    }
  }

}
</script>

<style scoped>

.loginbox{
    display:flex;
    position:absolute;
    width:900px;
    height:500px;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    box-shadow: 0 12px 16px 0  rgba(0,0,0,0.24), 0 17px 50px 0 #4E655D;
}
.loginbox-in{
     background-color:#89AB9E;
     width:240px;
}
.userbox{
    margin-top:120px ;
    height:30px;
     width:225px;
     display: flex;
     margin-left:25px;
}
.idbox{
    height:30px;
    width:230px;
    display: flex;
    margin-left:25px;
}
.pwdbox{
    height:30px;
    width:225px;
    display: flex;
    margin-left:25px;
}
.background{
    width:800px;
    background-image:url('./image/Christmas_Trees.png');
    background-size:cover;
    font-family:sans-serif;
}
.title{
    margin-top:400px;
    font-weight:bold;
    font-size:20px;
    color:#4E655D;
}
.title:hover{
     font-size:21px;
     transition: all 0.4s ease-in-out;
     cursor: pointer;
}
.uesr-text{
  position: relative;
  left: 0;
}
input{
    outline-style: none ;
    border: 0;
    border-bottom:1px solid #E9E9E9;
    background-color:transparent;
    height:20px;
     font-family:sans-serif;
    font-size:15px;
    color:#445b53;
    font-weight:bold;
}
 /* input::-webkit-input-placeholder{
    color:#E9E9E9;
 } */
input:focus{
    border-bottom:2px solid #445b53;
    background-color:transparent;
     transition: all 0.2s ease-in;
     font-family:sans-serif;
    font-size:15px;
     color:#445b53;
     font-weight:bold;
}
input:hover{
    border-bottom:2px solid #445b53;
    background-color:transparent;
     transition: all 0.2s ease-in;
     font-family:sans-serif;
    font-size:15px;
     color:#445b53;
     font-weight:bold;
}
input:-webkit-autofill {
  /* 修改默认背景框的颜色 */
 box-shadow: 0 0 0px 1000px  #89AB9E inset !important;
 /* 修改默认字体的颜色 */
 -webkit-text-fill-color: #445b53;
}
input:-webkit-autofill::first-line {
  /* 修改默认字体的大小 */
 font-size: 15px;
 /* 修改默认字体的样式 */
 font-weight:bold;
 }
.log-box{
    font-size:12px;
    display: flex;
    justify-content: space-between ;
    width:190px;
    margin-left:30px;
    color:#4E655D;
    margin-top:-5px;
    align-items: center;
}
.log-box-text{
    color:#4E655D;
    font-size:12px;
      text-decoration:underline;
    }
.login_btn{
    background-color: #5f8276; /* Green */
    border: none;
    color: #FAFAFA;
    padding: 5px 22px;
    text-align: center;
    text-decoration: none;
    font-size: 13px;
    border-radius: 20px;
    outline:none;
}
.login_btn:hover{
    box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
    cursor: pointer;
     background-color: #0b5137;
      transition: all 0.2s ease-in;
}

.warn{
    margin-top:60px;
    /* margin-right:120px; */
    margin-left:-120px;
    margin-bottom: 5px;
     font-weight:bold;
    font-size:17px;
}

.register_btn{
    background-color: transparent; /* Green */
    border: none;
    text-decoration: none;
    font-size: 12px;
    /* border-radius: 20px;   */
     color:#4E655D;
    font-size:12px;
    text-decoration:underline;
    display: flex;
    margin-left:25px;
    outline:none;

}
.register_btn:hover{
    font-weight:bold;
    cursor: pointer;
}
@font-face {
    font-family: "iconfont";
    src: url('./font/iconfont.eot');
    src: url('./font/iconfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
    url('./font/iconfont.woff2') format('woff2'), url('./font/iconfont.woff') format('woff'), url('./font/iconfont.ttf') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
    url('./font/iconfont.svg#iconfont') format('svg');
}

.iconfont {
    font-family: "iconfont",serif !important;
    font-size: 20px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    height:22px;
    color:#4E655D;
    margin-right:10px;
    margin-top:3px;
}

.icon-key:before {
    content: "\e775";
}

.icon-account:before {
    content: "\e817";
}

</style>
